<?php

/**
 * FCJ Members details database library
 * 
 * @author Felipe
 */
namespace FCJ\Database;

require_once ('php_Util_DB/php_Util_DB_PDO_lib.php');


/**
 * Namespace for PHP Util Database library
 *
*/
use php_Util\DB as ns_php_Util_DB;


use php_Util as ns_php_Util;

/**
 * Namespace for Database library
 *
 */
use FCJ\Database as ns_FCJ_Database;


/**
 * FCJ Members details table
 *
 */
class FCJ_DB_Members_Details_TABLE {

	public $tableName = 'fcj_members_Details';
	public $field_member_ID = 'member_ID';
	public $field_date_of_birth = 'date_of_birth';
	public $field_special_attentions = 'special_attentions';
	
}


/**
 * FCJ Member details data
 *
 */
class FCJ_DB_Member_Details_DATA {
	public $member_ID = null;
	public $date_of_birth = null;
	public $special_attentions = null;
}


/**
 * FCJ Members details editor.
 *
 * Provides CRUD facilities
 *
 *
 */
class FCJ_DB_Members_Details_EDITOR extends ns_php_Util_DB\php_Util_DB_EDITOR_PDO {

	/**
	 * @var FCJ_DB_Members_Details_TABLE
	 */
	protected $table_Description = null;


	public function get_Description_Table() {
		return $this->table_Description;
	}


	public function __construct($table_Description = null) {

		if ($table_Description != null) {
			$this->table_Description = $table_Description;
		} else {
			$this->table_Description = ns_FCJ_Database\FCJ_DB_Members_Details_FACTORY::get_NEW__FCJ_DB_Members_Details_TABLE();
		}
	}


	/**
	 * Return array of insert fields for SQL insert
	 * @param FCJ_DB_Member_Details_DATA $object_DATA
	 */
	public function get_Create_Fields($object_DATA) {

		$insertFields = array();

		$object_DATA->member_ID !== null ?  ns_php_Util\php_Util::array_push_associative($insertFields, array($this->table_Description->field_member_ID => $object_DATA->member_ID)) : null;
		$object_DATA->date_of_birth !== null ?  ns_php_Util\php_Util::array_push_associative($insertFields, array($this->table_Description->field_date_of_birth => $object_DATA->date_of_birth)) : null;
		$object_DATA->special_attentions !== null ?  ns_php_Util\php_Util::array_push_associative($insertFields, array($this->table_Description->field_special_attentions => $object_DATA->special_attentions)) : null;
		
		return $insertFields;
		
	}
 

	/**
	 * Return array of update fields for SQL insert
	 * @param FCJ_DB_Member_Details_DATA $object_DATA
	 */
	public function get_Update_Fields($object_DATA) {

		$object_DATA_Update = clone $object_DATA;

		$object_DATA_Update->member_ID = null;	// is a key. No update for this field.
		$updateFields = $this->get_Create_Fields($object_DATA_Update);

		return $updateFields;
	}


	/**
	 * Read DATA from row array
	 * @param FCJ_DB_Member_Details_DATA $object_DATA
	 * @param array $row_Array
	 */
	public function get_DATA_From_RowArray($object_DATA, $row_Array) {

		$object_DATA->member_ID = $row_Array[$this->table_Description->field_member_ID];
		$object_DATA->date_of_birth = $row_Array[$this->table_Description->field_date_of_birth];
		$object_DATA->special_attentions = $row_Array[$this->table_Description->field_special_attentions];
		
	}


	/**
	 * CRUD method for CREATE object
	 * @param FCJ_DB_Member_Details_DATA $object_DATA
	 * @throws \Exception
	 * @return int affected rows
	 *
	 */
	public function CRUD__Create($object_DATA) {

		// Check required parameters -----------------------------------------------------------------------|\/|---

		if (!is_a($object_DATA, "FCJ\Database\FCJ_DB_Member_Details_DATA")) {

			throw new \Exception("FCJ_DB_Member_Details_DATA is required.");
		}

		// -------------------------------------------------------------------------------------------------|/\|---


		// Create object -------------------------------------------------------------------------------------------------|\/|---
		$affected_Rows = $this->create_Object($this->table_Description->tableName, $this->get_Create_Fields($object_DATA));

		if ( $affected_Rows < 1 ) {
			throw new \Exception("Cannot create object.");
		}
		// ---------------------------------------------------------------------------------------------------------------|/\|---


		// Insert last insert ID in object ------------------------------------------|-|---
		$object_DATA->id = $this->db_Connection->_connection_Link->lastInsertId();


		return $affected_Rows;
	}


	/**
	 * CRUD method for READ object
	 * @param FCJ_DB_Member_Details_DATA $object_DATA
	 * @throws \Exception
	 *
	 */
	public function CRUD__Read($object_DATA) {

		// Check required parameters -----------------------------------------------------------------------|\/|---
		if (!is_a($object_DATA, 'FCJ\Database\FCJ_DB_Member_Details_DATA')) {
			throw new \Exception("FCJ_DB_Member_Details_DATA is required.");
		}

		// -------------------------------------------------------------------------------------------------|/\|---


		// Prepare conditions -----------------------------------------------------|\/|---
		$conditions_Array = array(
				$this->table_Description->field_member_ID => $object_DATA->member_ID
		);
		// ------------------------------------------------------------------------|/\|---



		// Read object ---------------------------------------------------------------------------------------------------|\/|---
		try {

			$result_DATA = $this->read_Object($this->table_Description->tableName, $conditions_Array);
			$this->get_DATA_From_RowArray($object_DATA, $result_DATA[0]);

		} catch (Exception $e) {
			throw new \Exception("Error reading object. ". $e->getMessage());
		}
		// ---------------------------------------------------------------------------------------------------------------|/\|---

	}


	/**
	 * CRUD method for UPDATE object
	 * @param FCJ_DB_Member_Details_DATA $object_DATA
	 * @throws \Exception
	 * @return int affected rows
	 *
	 */
	public function CRUD__Update($object_DATA) {

		// Check required parameters -----------------------------------------------------------------------|\/|---
		if (!is_a($object_DATA, 'FCJ\Database\FCJ_DB_Member_Details_DATA')) {
			throw new \Exception("FCJ_DB_Member_Details_DATA is required.");
		}
		// -------------------------------------------------------------------------------------------------|/\|---


		// Prepare conditions -----------------------------------------------------|\/|---
		$conditions_Array = array(
				$this->table_Description->field_member_ID => $object_DATA->member_ID
		);
		// ------------------------------------------------------------------------|/\|---


		// Update object ---------------------------------------------------------------------------------------------------------------------------|\/|---
		$affected_Rows = $this->update_Object($this->table_Description->tableName, $this->get_Update_Fields($object_DATA), $conditions_Array);

		if ( $affected_Rows < 1 ) {
			throw new \Exception("Cannot update object.");
		}
		// -----------------------------------------------------------------------------------------------------------------------------------------|/\|---


		return $affected_Rows;
	}


	/**
	 * CRUD method for DELETE object
	 * @param FCJ_DB_Member_Details_DATA $object_DATA
	 * @throws \Exception
	 * @return int affected rows
	 *
	 */
	public function CRUD__Delete($object_DATA) {

		// Check required parameters -----------------------------------------------------------------------|\/|---
		if (!is_a($object_DATA, 'FCJ\Database\FCJ_DB_Member_Details_DATA')) {
			throw new \Exception("FCJ_DB_Member_Details_DATA is required.");
		}
		// -------------------------------------------------------------------------------------------------|/\|---


		// Prepare conditions -----------------------------------------------------|\/|---
		$conditions_Array = array(
				$this->table_Description->field_member_ID => $object_DATA->member_ID
		);
		// ------------------------------------------------------------------------|/\|---


		// Delete object ---------------------------------------------------------------------------------------------------------------------------|\/|---
		$affected_Rows = $this->delete_Object($this->table_Description->tableName, $conditions_Array);

		if ( $affected_Rows < 1 ) {
			throw new \Exception("Cannot delete object.");
		}
		// -----------------------------------------------------------------------------------------------------------------------------------------|/\|---


		return $affected_Rows;
	}

}


/**
 *
 * FCJ Members details Database Factory
 *
 * Provides facilities for create Members details related objects
 *
 * @author Felipe
 *
 */
class FCJ_DB_Members_Details_FACTORY {


	public static function get_NEW__FCJ_DB_Members_Details_TABLE() {
		return new ns_FCJ_Database\FCJ_DB_Members_Details_TABLE();
	}


	public static function get_NEW__FCJ_DB_Member_Details_DATA() {
		return new ns_FCJ_Database\FCJ_DB_Member_Details_DATA();
	}


	public static function get_NEW__FCJ_DB_Members_Details_EDITOR() {
		return new ns_FCJ_Database\FCJ_DB_Members_Details_EDITOR();
	}


	/**
	 * Members details search facility by conditions
	 * @param array|string $conditions
	 * @param php_Util_DB_CONNECTION $connection
	 * @return array
	 *
	 * @throws \Exception
	 */
	public static function get__Members_by_Conditions($conditions, $connection) {

		$fcj_DB_Members_Details_EDITOR = self::get_NEW__FCJ_DB_Members_Details_EDITOR();
		$fcj_DB_Members_Details_TABLE = $fcj_DB_Members_Details_EDITOR->get_Description_Table();


		// Set connection -------------------------------------------------------|\/|---
		try {
			$fcj_DB_Members_Details_EDITOR->connect_To_DB($connection);
		} catch (\Exception $e) {
			throw new \Exception("Error in connection. ". $e->getMessage());
		}
		// ----------------------------------------------------------------------|/\|---


		// Get result array -----------------------------------------------------------------|\/|---
		$result_Array = array();

		try {
			$result_Array = $fcj_DB_Members_Details_EDITOR->read_Object($fcj_DB_Members_Details_TABLE->tableName, $conditions);
		} catch (\Exception $e) {
			throw new \Exception("Error reading object. ". $e->getMessage());
		}
		// ----------------------------------------------------------------------------------|/\|---


		// Get members details data from result array -------------------------------------------------------------------------------------|\/|---
		$array_Members_Details_DATA = array();

		for ($index_Result = 0; $index_Result < sizeof($result_Array); $index_Result++) {
			$fcj_DB_Member_Detail_DATA = ns_FCJ_Database\FCJ_DB_Members_Details_FACTORY::get_NEW__FCJ_DB_Member_Details_DATA();
			$fcj_DB_Members_Details_EDITOR->get_DATA_From_RowArray($fcj_DB_Member_Detail_DATA, $array_Members_Details_DATA[$index_Result]);
			array_push($array_Members_Details_DATA, $fcj_DB_Member_Detail_DATA);
		}
		// --------------------------------------------------------------------------------------------------------------------------------|/\|---


		return $array_Members_Details_DATA;

	}

}



